package  com.wang.head.controller;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.controller.BaseController;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.users.pojo.AAdmins;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IUsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class NoLoginController extends BaseController
{
    @Resource
    private IUsersService usersService;

    /**
     * 打开登陆页面
     * @return
     */
    @RequestMapping("/login")
    public String login()
    {
        ConstatFinalUtil.SYS_LOGGER.info("==login===");
        return "/head/login" ;
    }

    /**
     * 登陆提交页面
     * @return
     */
    @RequestMapping("/loginSubmit")
    public String loginSubmit(HttpServletRequest request)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==loginSubmit===");
        String info = "";

        HttpSession session = request.getSession() ;

        /* 接收参数 */
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        /* 要按照邮箱去数据库查询单条记录 */
        Map<String,Object> condMap = new HashMap<>();
        condMap.put("email",email);
        AUsers users = this.usersService.selectOneUsersService(condMap);
        if(users != null)
        {
            /* 用户存在 */
            if(users.getPassword().equalsIgnoreCase(password))
            {
                /* 密码正确 */
                if(users.getStatus() == 1)
                {
                    /* 登陆成功 */
                    /* 把登陆信息放到session中 */
                    session.setAttribute("users",users);
                    session.setAttribute("lastLoginTime",users.getLastLoginTime());

                    /* 更新上次登陆时间 */
                    users.setLastLoginTime(new Date());
                    JSONObject resultDbJSON = this.usersService.updateOneUsersService(users);
                    ConstatFinalUtil.SYS_LOGGER.info("==更新结果=={}",resultDbJSON.toJSONString());

                    /* 客户端跳转到登陆之后的网页 */
                    return "redirect:/head/users/main.mvc" ;
                }else
                {
                    info = "账户已经被禁用,请联系管理员";
                }

                request.setAttribute("email",users.getEmail());
            }else
            {
                info = "密码不正确" ;
            }
        }else
        {
            info = "账户不存在" ;
        }

        request.setAttribute("info",info);
        return this.login() ;
    }

    /**
     * 打开注册页面
     * @return
     */
    @RequestMapping("/register")
    public String register()
    {
        ConstatFinalUtil.SYS_LOGGER.info("==register===");
        return "/head/register" ;
    }

    /**
     * 注册提交页面
     * @return
     */
    @RequestMapping("/registerSubmit")
    public String registerSubmit(HttpServletRequest request)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==registerSubmit===");

        HttpSession session = request.getSession() ;

        /* 接收参数 */
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        AUsers users = new AUsers();

        users.setEmail(email);
        users.setPassword(password);
        users.setStatus(Byte.valueOf("1"));

        /* 设置用户的状态为启用 */

        /* 保存用户 */
        JSONObject resultJSON = this.usersService.saveOneUsersService(users);
        /* 保存信息 */
        this.info = resultJSON.getString("info");
        request.setAttribute("info",info);
        return this.register() ;
    }
}
